/*
@author: Edgar Pereira
Nº 6207
*/

#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>
#include <Python.h>
#include <string.h>
#include <locale.h>
#include "myDataProcessing.h"


//#############################################################################
// statistic with number of teachers per each year
//#############################################################################
PyObject* teachers_Year(){
	printf("Processing statistics with number of teachers\n\n");

	int i;
	sqlite3 *db;
	char *zErrMsg = 0;
	int rc;
	PyObject* theList = PyList_New(0);
	struct nTeachersYear result[10];
	
	int *contagem;
	FILE* file;
	file = fopen( "Rebides.txt", "w" );
	
	for (i = 0; i <  10; i++){

		char string[25] = "rebidesDB/rebides200";
        strcat(string, PyString_AsString(PyString_FromFormat("%d",i)));
        strcat (string, ".db");
        char* dbName = (string);
        char* sqlInst = "SELECT COUNT(*) FROM docentes";

        rc = sqlite3_open(dbName, &db);

        rc = sqlite3_exec(db, sqlInst, callback, &contagem, &zErrMsg);
        if( rc!=SQLITE_OK ){
                fprintf(stderr, "SQL error: %s\n", zErrMsg);
                sqlite3_free(zErrMsg);
        }

		sqlite3_close(db);
		result[i].year = i;
		result[i].number = contagem;
	}

	mYqSort(result, 10, sizeof(struct nTeachersYear), strucCompare);


	fprintf(file, "############################################################\n");
	fprintf(file, "#           Lista do nº de Professores Por Ano             #\n");
	fprintf(file, "#----------------------------------------------------------#\n");
	fprintf(file, "#    Organizado por ordem crescente do nº de professores   #\n");
	fprintf(file, "############################################################\n\n");

	for(i = 0; i < 10; i++){
		PyObject* tYear = PyTuple_New(2);
		PyTuple_SetItem( tYear, 0, PyInt_FromLong(result[i].year));
		PyTuple_SetItem( tYear, 1, PyInt_FromLong(result[i].number));
		PyList_Append(theList, tYear);

		fprintf(file, "+ O ano 200%d teve um total de : %d docentes\n", result[i].year, result[i].number);
		printf("+ O ano 200%d teve um total de : %d docentes\n", result[i].year, result[i].number);
	}

	fprintf(file, "\n############################################################\n\n");

	fclose(file);

	return theList;


} // end teachers_Year



//#############################################################################
// statistic with number of teachers per establishment each year
//#############################################################################
PyObject* teachEstYear(){
	printf("Processing statistics with number of teachers per establishment\n\n");
	setlocale( LC_ALL, "Portuguese" );

	int i = 0;
	int j = 0;
	int rc = 0;	
	int nEst = 0;
	sqlite3 *db;

	char *zErrMsg = 0;

	PyObject* theListTeachEst = PyList_New(0);
	PyObject* ListnEst = PyList_New(10);

	struct nTeachersEstabYear* resultTEY;
	struct nTeachersEstabYear* resultTEYAlias;
	

	FILE *file;
	file = fopen( "Rebides.txt", "a" );

	ListnEst = nEstabYear();

	fprintf(file, "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n");
	fprintf(file, "&  Estatistica do nº de Profs por Estabelecimentos por Ano &\n");
	fprintf(file, "&----------------------------------------------------------&\n");
	fprintf(file, "& Organizado por ordem crescente do nº de professores/ano  &\n");
	fprintf(file, "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n\n");

	for (i = 0; i <  10; i++){

		nEst = PyInt_AS_LONG(PyTuple_GetItem(PyList_GET_ITEM(ListnEst, 
		                                                     PyInt_AsSsize_t(PyInt_FromLong(i))),PyInt_AsSsize_t(PyInt_FromLong(1))));

		resultTEY = malloc(nEst*sizeof(struct nTeachersEstabYear));
		
		resultTEYAlias = resultTEY;
		
		char string[25] = "rebidesDB/rebides200";
		strcat(string, PyString_AsString(PyString_FromFormat("%d",i)));
		strcat (string, ".db");
		char* dbName = (string);

		char* sqlInst = "SELECT DISTINCT estabelecimento, COUNT(DISTINCT docente) AS number FROM fichas_docencia GROUP BY estabelecimento ORDER by number";

		rc = sqlite3_open(dbName, &db);

		rc = sqlite3_exec(db, sqlInst, callbackTuple, &resultTEY, &zErrMsg);
		if( rc!=SQLITE_OK ){
			fprintf(stderr, "SQL error: %s\n", zErrMsg);
			sqlite3_free(zErrMsg);
		}
		
		mYqSort(resultTEYAlias, nEst, sizeof(struct nTeachersEstabYear), strucnTeachersEstabYearCompare);

		fprintf(file, "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n");
		fprintf(file, "&                     Ano de 200%d:                        &\n", i);
		fprintf(file, "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n\n");
		printf("Year of 200%d:\n", i);

		for(j = 0; j < nEst; j++){
			
			PyObject* tTeachEstYear = PyTuple_New(3);
			PyTuple_SetItem(tTeachEstYear, PyInt_AsSsize_t(PyInt_FromLong(0)), PyInt_FromLong(i));
			PyTuple_SetItem(tTeachEstYear, PyInt_AsSsize_t(PyInt_FromLong(1)), PyString_FromFormat("%s",resultTEYAlias[j].estab));
			PyTuple_SetItem(tTeachEstYear, PyInt_AsSsize_t(PyInt_FromLong(2)), PyInt_FromLong(resultTEYAlias[j].number));
			PyList_Append(theListTeachEst, tTeachEstYear);

			fprintf(file, "%dº - %s: teve um total de : %d professores\n", (j + 1), resultTEYAlias[j].estab, resultTEYAlias[j].number);
			//printf("%dº - %s: teve um total de : %d professores\n", (j + 1), resultTEYAlias[j].estab, resultTEYAlias[j].number);
			free(resultTEYAlias[j].estab);
		}

		free(resultTEYAlias);
		printf("Year of 200%d OK!\n", i);				
		fprintf(file, "\n");
	}

	fprintf(file, "\n&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n\n");
	fclose(file);

	return theListTeachEst;		

} // end teachEstYear



//#############################################################################
// statistic with number of teachers per degree each year
//#############################################################################
PyObject* teachDegYear(){
	printf("Processing statistics with number of teachers per degree\n\n");
	setlocale( LC_ALL, "Portuguese" );

	int i = 0;
	int j = 0;
	int rc = 0;	
	int nDeg = 0;
	sqlite3 *db;

	char *zErrMsg = 0;

	PyObject* theListTeachDeg = PyList_New(0);
	PyObject* ListnDeg = PyList_New(10);

	struct nTeachersDegreeYear* resultTDY;
	struct nTeachersDegreeYear* resultTDYAlias;
	

	FILE *file;
	file = fopen( "Rebides.txt", "a" );

	ListnDeg = nDegreeYear();

	fprintf(file, "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n");
	fprintf(file, "&        Estatistica do nº de Profs por Grau por Ano       &\n");
	fprintf(file, "&----------------------------------------------------------&\n");
	fprintf(file, "& Organizado por ordem crescente do nº de professores/ano  &\n");
	fprintf(file, "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n\n");

	for (i = 0; i <  10; i++){

		nDeg = PyInt_AS_LONG(PyTuple_GetItem(PyList_GET_ITEM(ListnDeg, 
		                                                     PyInt_AsSsize_t(PyInt_FromLong(i))),PyInt_AsSsize_t(PyInt_FromLong(1))));

		resultTDY = malloc(nDeg*sizeof(struct nTeachersDegreeYear));

		resultTDYAlias = resultTDY;
		
		char string[25] = "rebidesDB/rebides200";
		strcat(string, PyString_AsString(PyString_FromFormat("%d",i)));
		strcat (string, ".db");
		char* dbName = (string);

		char* sqlInst = "SELECT DISTINCT grau, COUNT(DISTINCT docente) AS number FROM fichas_docencia GROUP BY grau ORDER by number";

		rc = sqlite3_open(dbName, &db);

		rc = sqlite3_exec(db, sqlInst, callbackTuple, &resultTDY, &zErrMsg);
		if( rc!=SQLITE_OK ){
			fprintf(stderr, "SQL error: %s\n", zErrMsg);
			sqlite3_free(zErrMsg);
		}

		mYqSort(resultTDYAlias, nDeg, sizeof(struct nTeachersDegreeYear), strucnTeachersDegreeYearCompare);

		fprintf(file, "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n");
		fprintf(file, "&                     Ano de 200%d:                        &\n", i);
		fprintf(file, "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n\n");
		printf("Year of 200%d:\n", i);

		for(j = 0; j < nDeg; j++){

			PyObject* tTeachDegreeYear = PyTuple_New(3);
			PyTuple_SetItem(tTeachDegreeYear, PyInt_AsSsize_t(PyInt_FromLong(0)), PyInt_FromLong(i));
			PyTuple_SetItem(tTeachDegreeYear, PyInt_AsSsize_t(PyInt_FromLong(1)), PyString_FromFormat("%s",resultTDYAlias[j].degree));
			PyTuple_SetItem(tTeachDegreeYear, PyInt_AsSsize_t(PyInt_FromLong(2)), PyInt_FromLong(resultTDYAlias[j].number));
			PyList_Append(theListTeachDeg, tTeachDegreeYear);
			fprintf(file, "%dº - %s: teve um total de : %d professores\n", (j + 1), resultTDYAlias[j].degree, resultTDYAlias[j].number);
			//printf("%dº - %s: teve um total de : %d professores\n", (j + 1), resultTDYAlias[j].degree, resultTDYAlias[j].number);
			free(resultTDYAlias[j].degree);
		}
		free(resultTDYAlias);
		
		fprintf(file, "\n");
		printf("Year of 200%d OK!\n", i);
		printf("\n");
	}

	fprintf(file, "\n&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n\n");
	fclose(file);

	return theListTeachDeg;		

} // end teachDegYear	



//#############################################################################
// statistic with number of teachers per degree and per establishment each year
//#############################################################################
PyObject* teachDegEstYear(){
	printf("Processing statistics with number of teachers per degree and per establishment\n\n");
	setlocale( LC_ALL, "Portuguese" );

	int i = 0;
	int j = 0;
	int rc = 0;	
	sqlite3 *db;

	char *zErrMsg = 0;

	PyObject* theListTeachDeg = PyList_New(0);

	int* degreeList = nEstabDeg();

	struct nTeachersDegreeEstYear* resultTDEY;
	struct nTeachersDegreeEstYear* resultTDEYAlias;
	
	FILE *file;
	file = fopen( "Rebides.txt", "a" );

	fprintf(file, "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n");
	fprintf(file, "& Estatistica do nº de Profs por Grau/Estabelecimento/Ano  &\n");
	fprintf(file, "&----------------------------------------------------------&\n");
	fprintf(file, "& Organizado por ordem crescente do nº de professores/ano  &\n");
	fprintf(file, "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n\n");

	for (i = 0; i <  10; i++){

		printf("\nList: %d \n", (degreeList[i]));

		resultTDEY = malloc((degreeList[i])*sizeof(struct nTeachersDegreeEstYear));

		resultTDEYAlias = resultTDEY;
		
		char string[25] = "rebidesDB/rebides200";
		strcat(string, PyString_AsString(PyString_FromFormat("%d",i)));
		strcat (string, ".db");
		char* dbName = (string);

		char* sqlInst = "SELECT DISTINCT grau, estabelecimento, COUNT(docente) AS total FROM fichas_docencia GROUP BY grau, estabelecimento";

		rc = sqlite3_open(dbName, &db);

		rc = sqlite3_exec(db, sqlInst, callbacknTeachersDegreeEstYear, &resultTDEY, &zErrMsg);
		if( rc!=SQLITE_OK ){
			fprintf(stderr, "SQL error: %s\n", zErrMsg);
			sqlite3_free(zErrMsg);
		}

		fprintf(file, "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n");
		fprintf(file, "&                     Ano de 200%d:                        &\n", i);
		fprintf(file, "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n\n");
		printf("Year of 200%d:\n", i);

		for(j = 0; j < degreeList[i]; j++){

			PyObject* tTeachDegreeEstYear = PyTuple_New(4);
			PyTuple_SetItem(tTeachDegreeEstYear, PyInt_AsSsize_t(PyInt_FromLong(0)), PyInt_FromLong(i));
			PyTuple_SetItem(tTeachDegreeEstYear, PyInt_AsSsize_t(PyInt_FromLong(1)), PyString_FromFormat("%s",resultTDEYAlias[j].degree));
			PyTuple_SetItem(tTeachDegreeEstYear, PyInt_AsSsize_t(PyInt_FromLong(2)), PyString_FromFormat("%s",resultTDEYAlias[j].estab));
			PyTuple_SetItem(tTeachDegreeEstYear, PyInt_AsSsize_t(PyInt_FromLong(3)), PyInt_FromLong(resultTDEYAlias[j].number));
			PyList_Append(theListTeachDeg, tTeachDegreeEstYear);

			fprintf(file, "%dº - %s - %s: %d professores\n", (j + 1), resultTDEYAlias[j].degree, resultTDEYAlias[j].estab, resultTDEYAlias[j].number);
			//printf("%dº - %s - %s: %d professores\n", (k + 1), resultTDEYAlias[j].degree, resultTDEYAlias[k].estab, resultTDEYAlias[k].number);

			free(resultTDEYAlias[j].degree);
			free(resultTDEYAlias[j].estab);
		}

		free(resultTDEYAlias);
		printf("Year of 200%d OK!\n", i);
		fprintf(file, "\n");
	}

	fprintf(file, "\n&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n\n");
	fclose(file);
	free(degreeList);

	return theListTeachDeg;		

} // end teachDegEstYear


//#############################################################################
// List the establishments on each year
//#############################################################################
PyObject* listEstabYear(){
	printf("Processing lists of establishments\n\n");
	setlocale( LC_ALL, "Portuguese" );

	int i = 0;
	int j = 0;
	int rc = 0;	
	int nEst = 0;
	sqlite3 *db;

	char *zErrMsg = 0;

	PyObject* theListEstYear = PyList_New(0);
	PyObject* listnEY = PyList_New(10);

	struct lEstabYear* resultEY;
	struct lEstabYear* resultEYAlias;
	

	FILE *file;
	file = fopen( "Rebides.txt", "a" );

	listnEY = nEstabYear();

	fprintf(file, "::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::\n");
	fprintf(file, ":           Lista dos Estabelecimentos por Ano             :\n");
	fprintf(file, ":----------------------------------------------------------:\n");
	fprintf(file, ":           Organizado por ordem alfabética/ano            :\n");
	fprintf(file, "::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::\n\n");

	for (i = 0; i <  10; i++){

		nEst = PyInt_AS_LONG(PyTuple_GetItem(PyList_GET_ITEM(listnEY, 
		                                                     PyInt_AsSsize_t(PyInt_FromLong(i))),PyInt_AsSsize_t(PyInt_FromLong(1))));

		resultEY = malloc(nEst*sizeof(struct lEstabYear));

		resultEYAlias = resultEY;
		
		char string[25] = "rebidesDB/rebides200";
		strcat(string, PyString_AsString(PyString_FromFormat("%d",i)));
		strcat (string, ".db");
		char* dbName = (string);

		char* sqlInst = "SELECT DISTINCT estabelecimento FROM fichas_docencia";

		rc = sqlite3_open(dbName, &db);

		rc = sqlite3_exec(db, sqlInst, callbackEstList, &resultEY, &zErrMsg);
		if( rc!=SQLITE_OK ){
			fprintf(stderr, "SQL error: %s\n", zErrMsg);
			sqlite3_free(zErrMsg);
		}
		
		mYqSort(resultEYAlias, nEst, sizeof(struct lEstabYear), strucnEstabYear);

		fprintf(file, "::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::\n");
		fprintf(file, ":                     Ano de 200%d                         :\n", i);
		fprintf(file, "::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::\n\n");
		printf("Year of 200%d\n", i);

		for(j = 0; j < nEst; j++){

			PyObject* tEstYear = PyTuple_New(2);
			PyTuple_SetItem(tEstYear, PyInt_AsSsize_t(PyInt_FromLong(0)), PyInt_FromLong(j));
			PyTuple_SetItem(tEstYear, PyInt_AsSsize_t(PyInt_FromLong(1)), PyString_FromFormat("%s",resultEYAlias[j].name));
			PyList_Append(theListEstYear, tEstYear);

			fprintf(file, "%dº - %s\n", (j + 1), resultEYAlias[j].name);
			//printf("%dº - %s\n", (j + 1), resultEYAlias[j].name);
			free(resultEYAlias[j].name);
		}

		free(resultEYAlias);
		printf("Year of 200%d OK!\n", i);
		
		fprintf(file, "\n");
		printf("\n");
	}

	fprintf(file, "\n::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::\n\n");
	fclose(file);

	return theListEstYear;		

} // listEstabYear


//#############################################################################
// List of holders of a degree
//#############################################################################
PyObject* listHolDegYear(){
	printf("Processing lists of holders of a degree\n\n");
	setlocale( LC_ALL, "Portuguese" );

	int i = 0;
	int j = 0;
	int rc = 0;	
	int nDeg = 0;
	sqlite3 *db;

	char *zErrMsg = 0;

	PyObject* theListTeachDegYear = PyList_New(0);
	int* ListTeachDeg;

	struct lHolDeg* resultLTEY;
	struct lHolDeg* resultLTEYAlias;
	

	FILE *file;
	file = fopen( "Rebides.txt", "a" );

	ListTeachDeg = nTeachDeg();

	fprintf(file, "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n");
	fprintf(file, "&              Lista de Profs por Grau por Ano             &\n");
	fprintf(file, "&----------------------------------------------------------&\n");
	fprintf(file, "&    Organizado por ordem alfabetica de professores/ano    &\n");
	fprintf(file, "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n\n");

	for (i = 0; i <  10; i++){

		nDeg = ListTeachDeg[i];

		resultLTEY = malloc(nDeg*sizeof(struct lHolDeg));

		resultLTEYAlias = resultLTEY;
		
		char string[25] = "rebidesDB/rebides200";
		strcat(string, PyString_AsString(PyString_FromFormat("%d",i)));
		strcat (string, ".db");
		char* dbName = (string);

		char* sqlInst = "SELECT docente, grau FROM fichas_docencia\
							GROUP BY docente, grau\
							ORDER BY docente, grau";
		
		rc = sqlite3_open(dbName, &db);

		rc = sqlite3_exec(db, sqlInst, callbackHolDeg, &resultLTEY, &zErrMsg);
		if( rc!=SQLITE_OK ){
			fprintf(stderr, "SQL error: %s\n", zErrMsg);
			sqlite3_free(zErrMsg);
		}
		
		mYqSort(resultLTEYAlias, nDeg, sizeof(struct lHolDeg), strucnLHolDeg);

		fprintf(file, "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n");
		fprintf(file, "$                     Ano de 200%d:                        &\n", i);
		fprintf(file, "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n\n");
		printf("Ano de 200%d:\n", i);

		for(j = 0; j < nDeg; j++){
			
			PyObject* tLTeachDegreeYear = PyTuple_New(3);
			PyTuple_SetItem(tLTeachDegreeYear, PyInt_AsSsize_t(PyInt_FromLong(0)), PyInt_FromLong(i));
			PyTuple_SetItem(tLTeachDegreeYear, PyInt_AsSsize_t(PyInt_FromLong(1)), PyString_FromFormat("%s",resultLTEYAlias[j].name));
			PyTuple_SetItem(tLTeachDegreeYear, PyInt_AsSsize_t(PyInt_FromLong(2)), PyString_FromFormat("%s",resultLTEYAlias[j].degree));
			PyList_Append(theListTeachDegYear, tLTeachDegreeYear);

			fprintf(file, "%dº - %s - %s professores\n", (j + 1), resultLTEYAlias[j].name, resultLTEYAlias[j].degree);
			//printf("%dº - %s - %s professores\n", (j + 1), resultLTEYAlias[j].name, resultLTEYAlias[j].degree);
			free(resultLTEYAlias[j].name);
			free(resultLTEYAlias[j].degree);
		}

		free(resultLTEYAlias);
		
		fprintf(file, "\n");
		printf("Ano de 200%d: OK\n", i);
		printf("\n");
	}

	fprintf(file, "\n&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n\n");
	fclose(file);

	return theListTeachDegYear;		

} // end of listHolDegYear

//#############################################################################
// List of leaving/new personnel per establishment each year
//#############################################################################
PyObject* teachLeavingEstYear(){
	printf("Processing list of leaving/new personnel per establishment each year\n\n");
	
	int i = 0;
	int j = 0;
	int k = 0;
	int nEstJ = 0;
	int nEstK = 0;
	int nEstMAX = 10;

	PyObject* theListTeachLeavingEst = PyList_New(0);
	PyObject* ListnEstab = PyList_New(10);

	struct nTeachersEstabYear* resultNTEY;
	struct nTeachersEstabYear* resultNTEYAlias;
	struct nTeachersEstabYear* listYear;
	struct nTeachersEstabYear* listYearA;
	struct nTeachersEstabYear* listYearAfter;
	struct nTeachersEstabYear* listYearAfterA;
	

	FILE *file;
	file = fopen( "Rebides.txt", "a" );

	fprintf(file, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
	fprintf(file, "<     Lista do nº de entradas/saidas por instituição/ano   >\n");
	fprintf(file, "<---------------------------------------------------------->\n");
	fprintf(file, "< Organizado por ordem crescente do nº de professores/ano  >\n");
	fprintf(file, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n\n");

	ListnEstab = nEstabYear();

	for (i = 0; i <  9; i++){

		fprintf(file, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
		fprintf(file, "<                     Ano de 200%d:                         >\n", i+1);
		fprintf(file, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n\n");

		printf("Year of 200%d:\n", i+1);
		
		nEstJ = PyInt_AS_LONG(PyTuple_GetItem(PyList_GET_ITEM(ListnEstab, 
		                                                     PyInt_AsSsize_t(PyInt_FromLong(i+1))),PyInt_AsSsize_t(PyInt_FromLong(1))));

		nEstMAX = nEstJ; //change the nEstMAX to the desired nº os institutions to compare

		listYearAfter = malloc(nEstMAX*sizeof(struct nTeachersEstabYear));//change nEstMAX to nEstJ to use entire list
		listYearAfter = nTeachEstYear(i+1, nEstMAX); //change nEstMAX to nEstJ to use entire list
		listYearAfterA = listYearAfter;

		nEstK = PyInt_AS_LONG(PyTuple_GetItem(PyList_GET_ITEM(ListnEstab, 
		                                                     PyInt_AsSsize_t(PyInt_FromLong(i))),PyInt_AsSsize_t(PyInt_FromLong(1))));

		listYear = malloc(nEstK*sizeof(struct nTeachersEstabYear)); // allocates nEstK memory
		listYear = nTeachEstYear(i, nEstK);
		listYearA = listYear;

		resultNTEY = malloc(nEstMAX*sizeof(struct nTeachersEstabYear)); // allocate memory only for nEstMAX institutions, change nEstMAX to nEstJ to use entire list	
		resultNTEYAlias = resultNTEY;
		
		for (j = 0; j < nEstMAX; j++){	// to use with the entire list
			
			
			resultNTEYAlias[j].estab = listYearAfterA[j].estab;
			resultNTEYAlias[j].number = 0;
			
			int eXistenZ = 0;

			for (k = 0; k < nEstK; k++){
				
				if (strcmp(listYearAfterA[j].estab, listYearA[k].estab)== 0){

					printf("%d- %s: %d of %d\n",j+1, listYearAfterA[j].estab, j+1, nEstJ);
					fprintf(file, "%d- %s: %d of %d\n",j+1, listYearAfterA[j].estab, j+1, nEstJ);
					eXistenZ += 1;
					
					resultNTEYAlias[j].number = (listYearAfterA[j].number - listYearA[k].number);

					if (resultNTEYAlias[j].number < 0){
						printf("   There were %d teachers leaving the institution\n", (-1*resultNTEYAlias[j].number));
						fprintf(file, "   There were %d teachers leaving the institution\n", resultNTEYAlias[j].number);
					} else if (resultNTEYAlias[j].number > 0){
						printf("   There were %d new teachers in the institution\n", resultNTEYAlias[j].number);
						fprintf(file, "   There were %d new teachers in the institution\n", resultNTEYAlias[j].number);
					} else if (resultNTEYAlias[j].number == 0){
						printf("   There were no changes in this institution\n");
						fprintf(file, "  There were no changes in this institution\n");
					}
				}

			}
			
			if (eXistenZ == 0){

				printf("%d- %s: %d of %d\n",j+1, listYearAfterA[j].estab, j+1, nEstJ);
				fprintf(file, "%d- %s: %d of %d\n",j+1, listYearAfterA[j].estab, j+1, nEstJ);
				resultNTEYAlias[j].number = listYearAfterA[j].number;
				printf("   In 200%d this institution didn't exist- there were created %d new openings.\n", i, resultNTEYAlias[j].number);
				fprintf(file, "   In 200%d this institution didn't exist- there were created %d new openings.\n", i, resultNTEYAlias[j].number);

			}
			
			
		}

		mYqSort(resultNTEYAlias, nEstMAX, sizeof(struct nTeachersEstabYear), strucnTeachersEstabYearCompare); //change nEstMAX to nEstJ to use entire list	

		for(j = 0; j < nEstMAX; j++){	// //change nEstMAX to nEstJ to use entire list			

			PyObject* tListTeachLeavingEst = PyTuple_New(3);
			PyTuple_SetItem(tListTeachLeavingEst, PyInt_AsSsize_t(PyInt_FromLong(0)), PyInt_FromLong(i));
			PyTuple_SetItem(tListTeachLeavingEst, PyInt_AsSsize_t(PyInt_FromLong(1)), PyString_FromFormat("%s",resultNTEYAlias[j].estab));
			PyTuple_SetItem(tListTeachLeavingEst, PyInt_AsSsize_t(PyInt_FromLong(2)), PyInt_FromLong(resultNTEYAlias[j].number));
			PyList_Append(theListTeachLeavingEst, tListTeachLeavingEst);
		}

		free(resultNTEYAlias);
		free(listYearAfterA);
		free(listYearA);
		printf("Year of 200%d OK!\n", i+1);				
		fprintf(file, "\n");
	}

	fprintf(file, "\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n\n");
	fclose(file);

	return theListTeachLeavingEst;		

} // end teachLeavingEstYear
	